import csv
import pytest
import requests
#定义一个测试联调的类
import json
#@pytest.fixture(scope="function")
#成功登录
@pytest.fixture()
def test_successlogin():
        #参数化
        # file=open("data.csv","r")
        # file1=open("test.csv","a",newline='')
        # table=csv.reader(file)
        #登录接口的请求参数（字典格式）
        dldata={"user_name":"1154605217@qq.com","password":"123456"}
        print(type(dldata))
        #python字典格式转换成json格式
        info = json.dumps(dldata)
        print(type(info))
        #请求头
        header= {"Content-Type": "application/json"}
        url="https://background-test.uofferglobal.com/backend/api/v1/User/login"
        #发起post请求
        response = requests.post(url,headers=header,data=info).text
        # print(type(response))
        #将接口返回值转换成python字典格式
        dic = json.loads(response)
        # print(dic)
        # print(type(dic))
        #通过字典的取值方法提取返回所要的值
        Authorization=dic['data']
        print(Authorization)
        #返回值中寻找预期值，进行结果判定
        loc = response.find('ok')
        if (loc > 0):
            print("登录成功")
            # file1.write(rows[0]+","+rows[1]+","+"测试通过"+"\n")
        else:
            print("登录失败")
            # file1.write(rows[0]+","+rows[1]+","+"测试失败"+"\n")
        # file.close()
        #将登录接口返回的Authorization返回，然后下个接口进行使用
        return Authorization

